//Documentation complete
/**
 * @author Kazu
 * @version 2009FE09
 */

package problemDomain;

import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.*;

/**
 * A <code>Bean</code> which represents a food item that can be placed on a menu. 
 */
public class FoodItemBean implements Bean {
	
	/* Constants */
	
	private static final long serialVersionUID = 1622084296016383537L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	protected int id;
	protected double cost;
	protected String type;
	protected String name;
	protected String description;
	protected Boolean available;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>FoodItemBean</code> with default values.
	 */
	public FoodItemBean () {
		this.id = 0;
		this.name = "";
		this.description = "";
		this.cost = 0.0;
		this.type = "";
		this.available = null;
	}
	
	/**
	 * Constructs a <code>FoodItemBean object with a specified ID number.
	 * @param id ID number of the food item
	 */
	public FoodItemBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs an <code>FoodItemBean</code> with all values specified.
	 * @param id ID number of the food item
	 * @param name name of the food item
	 * @param description description of the food item
	 * @param cost base cost of the food item
	 * @param type type of the food item
	 * @param available availability of the food item
	 */
	public FoodItemBean (int id, String name, String description, double cost,
			String type, Boolean available)	{
		this.id = id;
		this.name = name;
		this.description = description;
		this.cost = cost;
		this.type = type;
		this.available = available;
	}
	
	/* END Constructors */
	
	
	/* Accessors */

	/**
	 * Returns the ID of the food item.
	 * @return id
	 */
	public final int getId () {
		return id;
	}
	
	/**
	 * Returns the name of the food item
	 * @return name
	 */
	public final String getName () {
		return name;
	}
	
	/**
	 * Returns the description of the food item
	 * @return description
	 */
	public final String getDescription () {
		return description;
	}
	
	/**
	 * Returns the base cost of the food item.
	 * @return base cost of the item
	 */
	public double getCost () {
		return cost;
	}
	
	/**
	 * Returns the type of the food item
	 * @return type of the item
	 */
	public String getType () {
		return type;
	}
	
	/**
	 * Returns <code>true</code> if the food item is available,
	 * <code>false</code> if the item is not available, or
	 * <code>null</code> if the availability is not defined or set. 
	 * @return <code>true</code> if the item is available, <code>false</code> if not, or <code>null</code> otherwise
	 */
	public Boolean getAvailability () {
		return available;
	}
	
	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the ID number of the food item. 
	 * @param id the ID number of food item to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets the name of the food item.
	 * @param name the name of the food item to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the description of the food item.
	 * @param description the description of the food item to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * Sets the base cost of the food item.
	 * @param cost the base cost of the food item
	 */
	public void setCost (double cost) {
		this.cost = cost;
	}
	
	/**
	 * Sets the type of the food item.
	 * @param type the food item type
	 */
	public void setType (String type) {
		this.type = type;
	}
	
	/**
	 * Sets the availability of the food item.
	 * @param available the food item availability
	 */
	public void setAvailability (Boolean available) {
		this.available = available;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throwError(
				ITEM_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (name != null && name.length() > 25)
			throwError(
				ITEM_NAME_TOO_LONG,
				name,
				this);
		
		if (description != null && description.length() > 255)
			throwError(
				ITEM_DESCRIPTION_TOO_LONG,
				description,
				this);
		
		if (cost < 0)
			throwError(
				ITEM_COST_INVALID,
				String.valueOf(cost),
				this);
		
		if (available == null)
			throwError(
				ITEM_AVAILABILITY_NULL,
				this);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<fooditem>");
		sb.append("<id>" + id + "</id>");
		sb.append("<name>" + name + "</name>");
		sb.append("<description>" + description + "</description>");
		sb.append("<cost>" + cost + "</cost>");
		sb.append("<type>" + type + "</type>");
		sb.append("<available>" + available + "</available>");
		sb.append("</fooditem>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + '\n');
		sb.append("Item Name: " + name + '\n');
		sb.append("Item Description: " + description + '\n');
		sb.append("Cost: " + cost + '\n');
		sb.append("Item Type: " + type + '\n');
		sb.append("Available: " + available + "\n\n");
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
